// pages/history/history_test.js

var wxCharts = require('../../utils/wxcharts.js');

var app = getApp();

var lineChart = null;//一个曲线图对象

var lineChart1 = null;

var util = require('../../utils/time.js');//加载自己设计的time.js模块

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  //手指触摸动作开始
  touchHandler: function (e) 
  {
    console.log(lineChart.getCurrentDataIndex(e));
    lineChart.showToolTip(e, {
        // background: '#7cb5ec',
        format: function (item, category) {
            return category + ' ' + item.name + ':' + item.data 
        }
    });
  }, 

createSimulationData: function () {
  var categories = [];
  var data = [];
  var data1 = [];

  if(app.globalData.enough_ten_flag == false)
  {
    for (var i = 0; i < getApp().globalData.idx; i++) 
    {
      categories.push(getApp().globalData.ten_time[i]);
      //data.push(Math.random()*(20-10)+10);
      data.push(getApp().globalData.ten_tempeature[i]);
      data1.push(getApp().globalData.ten_humi[i]);
      //console.log(getApp().globalData.ten_tempeature[i]);
    }
  }
  else 
  {
    for (var i = 0; i < 10; i++) 
    {
      categories.push(getApp().globalData.ten_time[i]);
      //data.push(Math.random()*(20-10)+10);
      data.push(getApp().globalData.ten_tempeature[i]);
      data1.push(getApp().globalData.ten_humi[i]);
      //console.log(getApp().globalData.ten_tempeature[i]);
    }
  }
  
  // data[4] = null;
  return {
      categories: categories,
      data: data,
      data1:data1
  }
},

createSimulationData1: function () {
  var categories = [];
  var data = [];
  var data1 = [];

  if(app.globalData.enough_ten_flag1 == false)
  {
    for (var i = 0; i < getApp().globalData.idx1; i++) 
    {
      categories.push(getApp().globalData.ten_time1[i]);
      data.push(getApp().globalData.ten_air[i]);
    }
  }
  else 
  {
    for (var i = 0; i < 10; i++) 
    {
      categories.push(getApp().globalData.ten_time1[i]);
      data.push(getApp().globalData.ten_air[i]);
    }
  }

  if(app.globalData.enough_ten_flag2 == false)
  {
    for (var i = 0; i < getApp().globalData.idx2; i++) 
    {
      //categories.push(getApp().globalData.ten_time[i]);
      data1.push(getApp().globalData.ten_ppb[i]);
    }
  }
  else
  {
    for (var i = 0; i < 10; i++) 
    {
      data1.push(getApp().globalData.ten_ppb[i]);
    }
  }
  
  // data[4] = null;
  return {
      categories: categories,
      data: data,
      data1:data1
  }
},

updateData: function () {
  var simulationData = this.createSimulationData();
  var simulationData1 = this.createSimulationData1();
  var series = 
  [
    {
      name: '温度',
      data: simulationData.data,
      //data1:simulationData.data1
      // format: function (val, name) {
      //     return val.toFixed(2) + '万';
      // }
    },

    { 
      name: '湿度',
      data : simulationData.data1
    },

  ];

  var series1 = 
  [
    {
      name: '空气粉尘',
      data: simulationData1.data,
    },
    {
      name: '甲醛浓度',
      data: simulationData1.data1,
    },
  ];

  lineChart.updateData({
      categories: simulationData.categories,
      series: series
  });

  lineChart1.updateData({
    categories:simulationData1.categories,
    series:series1
  });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var windowWidth = 320;
    //     try {
    //         var res = wx.getSystemInfoSync();//同步获取系统信息
    //         windowWidth = res.windowWidth;
    //         console.log(windowWidth);
    //     } catch (e) {
    //         console.error('getSystemInfoSync failed!');
    //     }
        
        var simulationData = this.createSimulationData()//获取信息函数

        var simulationData1 = this.createSimulationData1();

        lineChart = new wxCharts({
            canvasId: '1',//微信小程序canvas-id
            type: 'line',//图表类型
            categories: simulationData.categories,//数据类别分类
            animation: true,
            // background: '#f5f5f5',
            series: [ //数据列表
              {
                name: '温度',
                data: simulationData.data,
              
            }, 
            {
              name:'湿度',
              data: simulationData.data1,
            }
          ],
            xAxis://X轴设置
            {
                disableGrid: true//不绘制X轴网格
            },
            yAxis: 
            {
                title: '数值',
                format: function (val) {
                    return val.toFixed(2);
                },
                min: 0 //Y轴起始值
            },
            width: 316,//canvas宽度
            height: 150,//canvas高度
            dataLabel: true,//在图标中显示数据内容值
            dataPointShape: true,//在图表中显示数据点图形标识
            extra: {
                lineStyle: 'curve'//曲线
            }
        });

        lineChart1 = new wxCharts({
            canvasId: '2',//微信小程序canvas-id
            type: 'line',//图表类型
            categories: simulationData.categories,//数据类别分类
            animation: true,
            // background: '#f5f5f5',
            series: [ //数据列表
              {
                name: '空气粉尘',
                data: simulationData1.data,
              
            }, 
            {
              name:'甲醛浓度',
              data: simulationData.data1,
            }
          ],
            xAxis://X轴设置
            {
                disableGrid: true//不绘制X轴网格
            },
            yAxis: 
            {
                title: '数值',
                format: function (val) {
                    return val.toFixed(2);
                },
                min: 0 //Y轴起始值
            },
            width: 316,//canvas宽度
            height: 150,//canvas高度
            dataLabel: true,//在图标中显示数据内容值
            dataPointShape: true,//在图表中显示数据点图形标识
            extra: {
                lineStyle: 'curve'//曲线
            }
        });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})